<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper">
  <!-- Content Header (Page header) -->
  <section class="content-header">
    <h1>
      应用程序上线
      <small>注意环境选择</small>
    </h1>

    <ol class="breadcrumb">
      <li><a href="#"><i class="fa fa-dashboard"></i> Level</a></li>
      <li class="active">Here</li>
    </ol>
  </section>

  <!-- Main content -->
  <section class="content">

    <div class="row">
      <div class="col-xs-12">

        <%= form_tag '/deploy/%s' % @app.id do %>
            <div class="box box-primary">
              <div class="box-header">
                <h3 class="box-title">应用名：<%= @app.name %></h3>
              </div>
              <!-- /.box-header -->
              <div class="box-body">
                <ul class="nav nav-tabs">
                  <li class="active" id="svnSelect">
                    <a href="#svn" data-toggle="tab">
                      SVN
                    </a>
                  </li>
                  <li id="gitSelect"><a href="#git" data-toggle="tab"  >GIT</a></li>
                  <input name="svn_git" value="svn" type="hidden">
                </ul>
                <div class="tab-content">
                  <div class="tab-pane fade in active" id="svn">
                    <div class="form-group<%= @svn_address_error ? ' has-error' : '' %>">
                      <label>SVN 地址</label>
                      <input type="text" class="form-control" name="svn_address" id="svn_address" placeholder="可以把svn地址和版本号一起复制到这里，能够自动识别" value="<%= params[:svn_address] %>" autocomplete="off">
                    </div>
                    <div class="form-group<%= @svn_revision_error ? ' has-error' : '' %>">
                      <label>SVN 版本号</label>
                      <input type="text" class="form-control" name="svn_revision" placeholder="SVM revision" value="<%= params[:svn_revision] %>" autocomplete="off">
                    </div>
                  </div>
                  <div class="tab-pane fade" id="git">
                    <div class="form-group<%= @git_address_error ? ' has-error' : '' %>">
                      <label>GIT 地址</label>
                      <input type="text" class="form-control" name="git_address" id="git_address" placeholder="git地址" value="<%= params[:git_address] %>" autocomplete="off">
                    </div>
                    <div class="form-group<%= @git_revision_error ? ' has-error' : '' %>">
                      <label>GIT 版本号</label>
                      <input type="text" class="form-control" name="git_revision" placeholder="git 版本号 或 git 分支名（会使用该分支的最新版本号）" value="<%= params[:git_revision] %>" autocomplete="off">
                    </div>
                  </div>
                </div>

                <!-- radio -->
                <div class="form-group">
                  <label>环境选择</label>
                  <div class="radio">
                    <label>
                      <input type="radio" name="env" value="<%= DeployToolUtil.env_test%>" checked>
                      <%= DeployToolUtil.env_test%>
                    </label>
                  </div>
<!--                  <% if current_user.can_deploy_jicheng? %>
                  <div class="radio">
                        <label>
                          <input type="radio" name="env" value="<%= DeployToolUtil.env_jicheng%>">
                          <%= DeployToolUtil.env_jicheng%>
                        </label>
                      </div>
                  <%end%>-->
                  <% if current_user.can_deploy_predeploy? %>
                      <div class="radio">
                        <label>
                          <input type="radio" name="env" value="<%= DeployToolUtil.env_pre%>">
                          <%= DeployToolUtil.env_pre%>
                        </label>
                      </div>
                  <% end %>
                  <% if current_user.can_deploy_huidu? %>
                      <div class="radio">
                        <label>
                          <input type="radio" name="env" value="<%= DeployToolUtil.env_huidu%>">
                          <%= DeployToolUtil.env_huidu%>
                        </label>
                      </div>
                  <% end %>
                  <% if current_user.is_admin? or (current_user.can_deploy_production? and is_deploy_time?)%>
                      <div class="radio">
                        <label>
                          <input type="radio" name="env" value="<%= DeployToolUtil.env_production%>">
                          <%= DeployToolUtil.env_production%>
                        </label>
                      </div>
                  <% end %>
                </div>
              </div>
              <!-- /.box-body -->

              <div class="box-footer">
                <button type="submit" class="btn btn-primary" id="btn_submit">上线</button>
              </div>
            </div>
            <!-- /.box -->
        <% end %>
      </div>
      <!-- /.col -->
    </div>
    <!-- /.row -->-
  </section>
  <!-- /.content -->
</div><!-- /.content-wrapper -->

<script type="text/javascript">
    isFirstClick = true

    $('.btn-primary').click(function () {
        //生产环境部署要求额外确认
        if ($(".radio").find("input:checked").val() == '生产') {
            var re = window.confirm('该操作会部署生产环境，确认继续吗？')
            if (!re) {
                return false
            }
            re = window.confirm('该操作会部署生产环境，确认继续吗？')
            if (!re) {
                return false
            }
        }
        //通过js防止重复提交
        if (isFirstClick) {
            isFirstClick = false
        } else {
            return false
        }
    })

    //#ctrl+v自动复制剪切板中的版本信息
    $('input').keyup(function () {
        var data = $(this).val();
        if (data.split(" ").length == 2) {
            var data_array = data.split(" ");
            svn_address = data_array[0].replace(/^\s*/g, "")
            revision = data_array[1].replace(/\t+/, "")
            //验证数据是否符合格式，如果符合格式，自动赋值到2个input中
            if (svn_address.match(/^http:|svn:/)
                && revision.match(/^\d+$/)) {
                $('input[name="svn_address"]').val(svn_address)
                $('input[name="svn_revision"]').val(revision)
            }
        }
    })

    $('#svnSelect').click(function () {
        $('input[name="svn_git"]').val("svn");
    })
    $('#gitSelect').click(function () {
        $('input[name="svn_git"]').val("git");
    })
</script>